package hk.controller;

import hk.beans.Account;
import hk.common.R;
import hk.enums.EnumStatus;
import hk.service.AccountService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/24 19:39
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    AccountService accountService;

    @Value("${server.port}")
    private int port;

    @PostMapping("/addaccount")
    public R<?> addPayment(@RequestBody Account account) {
        accountService.addAccount(account);
        return new  R<>( EnumStatus.OK.getValue()  ,EnumStatus.OK.getMsg(),null);
    }

    @GetMapping("/getaccount/{account}")
    public R<Account> get(@PathVariable("account") String accountId) throws ParseException {
        Account account=accountService.getAccountById(accountId);
        //处理时间戳 失败
//        Date editTimeDate = account.getEditTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedEditTime = sdf.format(editTimeDate);
//        Date date = sdf.parse(formattedEditTime);
//        System.out.println("date = " + date);
//        account.setEditTime(date);
        if(account!=null){
            return new R<>(EnumStatus.OK.getValue(),EnumStatus.OK.getMsg()+"当前服务器："+port,account);
        }else{
            return new R<>(EnumStatus.NOT_FOUND.getValue(), EnumStatus.NOT_FOUND.getMsg()+"当前服务器："+port, null);
        }
    }

    @GetMapping("/getaccount")
    public R<List<Account>> getAllMsg() {
        List<Account> allMsg = accountService.getAllMsg();
        if(allMsg!=null){
            return new R<>(EnumStatus.OK.getValue(),EnumStatus.OK.getMsg()+"当前服务器："+port,allMsg);
        }else{
            return new R<>(EnumStatus.NOT_FOUND.getValue(), EnumStatus.NOT_FOUND.getMsg()+"当前服务器："+port, null);
        }
    }

    @GetMapping("/getallmoney")
    public R<String> getAllMoney() {
        String allMoney = String.valueOf(accountService.getAllMoney());
        if(allMoney!=null){
            return new R<>(EnumStatus.OK.getValue(),EnumStatus.OK.getMsg()+"当前服务器："+port+"总额为："+allMoney,null);
        }else{
            return new R<>(EnumStatus.NOT_FOUND.getValue(), EnumStatus.NOT_FOUND.getMsg()+"当前服务器："+port, null);
        }
    }

    @GetMapping("/delete/{id}")
    public R<Integer> delete(@PathVariable("id") Integer id) throws ParseException {
        accountService.delete(id);
        return new R<>(EnumStatus.OK.getValue(),EnumStatus.OK.getMsg()+"当前服务器："+port+",删除的id为："+id,id);

    }
}
